$('document').ready(function() {
    var left = 0;
    var add = 10;
    var height = 0;
    var top = 0;
    var jumpBool = false;
    
    $('body').append('<div id="info"><span id="left"></span> <span id="top"></span> <span id="key"></span> <span id="colAlert"></span></div>');
    
    $('body').keydown(function(event) {
        var code = event.keyCode;
        $('#key').html('<b>Key:</b>' + code);
        move(code);
    })
    
    function move(key) {
        
        var collisionDetected = {top: false, left: false, right: false, bottem: false}
        var collisionObject = null;
        
        if(key == 65) {
            if(!collisionDetected.left) {
                left = left-add;
            }
        }
        
        if(key == 68) {
            if(!collisionDetected.right) {
               left = left+add; 
            }
            
        }
        
        if(key == 87) {
            if(!collisionDetected.bottem) {
                top = top-add; 
            }
        }
        
        if(key == 83) {
            if(!collisionDetected.top) {
                top = top+add; 
            }
        }
        
        if(key == 32) {
            myObj.init();
            if(!jumpBool) {
                jump();   
            }
        }
        
        $('#player').css({left: left+"px", top: top + 'px'}, "fast");
        $('#left').html('<b>Left:</b> ' + left + 'px' + ' <b>Top: </b>' + top + 'px');
        var collisionCheck = collision.detect();
        
        if(collisionCheck.detectedCollision) {
           collisionDetected = collisionCheck.collisionType;
           collisionObject = collisionCheck.collisionObject;
        };
    }
    
    function jump() {
        jumpBool = true;
        $('#player').animate({bottom: 30 + 'px'}, 400, function() {
            $(this).animate({bottom: '0px'}, 200);
            jumpBool = false;
        })   
    }
    /*
    function collisionDetection() {
        //Player object information
        var playerWith = parseInt($('#player').css("width"));
        var playerHeight = parseInt($('#player').css("height"));
        var playerPosX = parseInt($('#player').css("left"));
        var playerPosY = parseInt($('#player').css("top"));
        
        //Playerpoints
        var playerPointTopLeft = {x: playerPosX, y: playerPosY};
        var playerPointTopRight = {x: playerPosX + playerWith, y: playerPosY}
        var playerPointBotLeft = {x: playerPosX, y: playerPosY + playerHeight}
        var playerPointBotRight = {x: playerPosX + playerWith, y: playerPosY + playerHeight};
        
        //Setting playerpoints
        $('#playerPoint1').css({left: playerPointTopLeft.x + 'px' ,top: playerPointTopLeft.y + 'px' });
        $('#playerPoint2').css({left: playerPointTopRight.x + 'px' ,top: playerPointTopRight.y + 'px' });
        $('#playerPoint3').css({left: playerPointBotLeft.x + 'px' ,top: playerPointBotLeft.y + 'px' });
        $('#playerPoint4').css({left: playerPointBotRight.x + 'px' ,top: playerPointBotRight.y + 'px' }); 
        
        //Collision switch
        var collisionAlert = false;
        var collisionType = {top: false, left: false, bottom: false, right: false};
        
        $('.colobject').each(function() {
                    //Collision object information
                    var objectWith = parseInt($(this).css("width"));
                    var objectHeight = parseInt($(this).css("height"));
                    var objectPosX = parseInt($(this).css("left"));
                    var objectPosY = parseInt($(this).css("top"));
        
                    //Collision object points
                    var objectPointTopLeft = {x: objectPosX, y: objectPosY};
                    var objectPointTopRight = {x: objectPosX + objectWith, y: objectPosY}
                    var objectPointBotLeft = {x: objectPosX, y: objectPosY + objectHeight}
                    var objectPointBotRight = {x: objectPosX + objectWith, y: objectPosY + objectHeight};
                    
                    //Comming from left side
                    if(playerPointTopRight.x >= objectPointTopLeft.x && playerPointTopLeft.x <= objectPointTopRight.x) {
                        if(playerPointBotRight.y >= objectPointTopRight.y && playerPointTopRight.y <= objectPointBotRight.y) {
                            collisionAlert = true;
                            
                            if(playerPointBotRight.y == objectPointTopRight.y) {
                                collisionType.top = true;
                            }  else if(playerPointTopRight.y == objectPointBotRight.y) {
                                collisionType.bottem = true;
                            } else if(playerPointTopRight.x == objectPointTopLeft.x) {
                                collisionType.right = true; 
                            } else if(playerPointTopLeft.x == objectPointTopRight.x) {
                                collisionType.left = true;
                            }
                        }
                    }          
        })
        
        if(collisionAlert) {
            $('#colAlert').html("Collision!");
        } else {
            $('#colAlert').html("No Collision!")
        }
        
        return collisionType;
    }
    */
    
})

//window.onbeforeunload = function() {
//    return "Vil du virkelig forlade siden?";
//}